package com.leilei.count;

import com.alibaba.fastjson.JSON;
import com.leilei.Location;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.windowing.RichAllWindowFunction;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;
import org.apache.flink.util.Collector;

import java.util.UUID;

/**
 * @author lei
 * @version 1.0
 * @desc 无key 窗口，则并行度为1
 * @date 2021-03-16 10:50
 */
public class CountNoKeyWindowFunction extends RichAllWindowFunction<Location, String, GlobalWindow> {
    String uuid;

    @Override
    public void apply(GlobalWindow window, Iterable<Location> locations, Collector<String> out) throws Exception {
        for (Location location : locations) {
            if (location.getGpsSpeed() > location.getLimitSpeed()) {
                out.collect(JSON.toJSONString(location));
            }
        }
    }

    @Override
    public void open(Configuration parameters) throws Exception {
        uuid = UUID.randomUUID().toString();
        System.out.println(uuid + "窗口打开了");
    }
}
